package com.hdc.mumuwei.micro_account_server.service.impl;

import com.hdc.mumuwei.micro_account_server.mapper.PayTypeMapper;
import com.hdc.mumuwei.micro_account_server.po.Paging;
import com.hdc.mumuwei.micro_account_server.po.PayType;
import com.hdc.mumuwei.micro_account_server.service.PayTypeService;
import com.hdc.mumuwei.micro_account_server.utils.Result;
import com.hdc.mumuwei.micro_account_server.utils.TimeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Mumuwei
 * @Description
 * @create 2021/11/19 22:48
 */
@Service
public class PayTypeServiceImpl implements PayTypeService {

    @Autowired
    PayTypeMapper payTypeMapper;

    Result result;

    /**
     * 获取分类列表（分页）
     *
     * @param paging 分页参数
     * @return 分类列表
     */
    @Override
    public <T> Result getTypeList(Paging paging) {
        Map<String, Object> data = new HashMap();
        paging.setStart((paging.getCurrent()-1)* paging.getSize());
        int total = payTypeMapper.getTypeTotal(paging);
        data.put("total", total);
        List<PayType> list = payTypeMapper.getTypeList(paging);
        data.put("list", list);
        if(list!= null){
            result = new Result<>(data);
        }else {
            result = new Result<>(Result.ERR_CODE,Result.ERR_MSG);
        }
        return result;
    }

    /**
     * 获取分类列表（不分页）
     * @return 分类列表
     */
    @Override
    public <T> Result getAllTypes() {
        Map<String, Object> data = new HashMap();
        int total = payTypeMapper.getTypeTotal(null);
        data.put("total", total);
        List<PayType> list = payTypeMapper.getAllTypes();
//        list.addAll(0, (Collection<? extends PayType>) new PayType());
        data.put("list", list);
        if(list!= null){
            result = new Result<>(data);
        }else {
            result = new Result<>(Result.ERR_CODE,Result.ERR_MSG);
        }
        return result;
    }

    /**
     * 添加分类
     *
     * @param payType 分类信息
     * @return 影响行数
     */
    @Override
    public <T> Result addType(PayType payType) {
        PayType find = payTypeMapper.findTypeByName(payType.getName());
        if(find == null){
            payType.setCreateTime(TimeUtils.getCurrentTimes());
            int row = payTypeMapper.addType(payType);
            if(row>0){
                result = new Result<>(Result.SUCCESS_CODE, Result.ADD_OK_MSG);
            }else {
                result = new Result<>(Result.ERR_CODE, Result.ERR_MSG);
            }
        }else {
            result = new Result<>(Result.ERR_EXIST_CODE, Result.TYPE_EXIST_MSG);
        }
        return result;
    }

    /**
     * 更新分类
     *
     * @param payType 分类参数
     * @return 影响行数
     */
    @Override
    public <T> Result updateType(PayType payType) {
        int row = payTypeMapper.updateType(payType);
        if(row>0){
            result = new Result<>(Result.SUCCESS_CODE, Result.UPDATE_OK_MSG);
        }else {
            result = new Result<>(Result.ERR_CODE, Result.ERR_MSG);
        }
        return result;
    }

    /**
     * 删除分类
     *
     * @param tid 分类id
     * @return 返回对象
     */
    @Override
    public <T> Result delType(Integer tid) {
        int row = payTypeMapper.delType(tid);
        if(row>0){
            result = new Result<>(Result.SUCCESS_CODE, Result.DELETE_OK_MSG);
        }else {
            result = new Result<>(Result.ERR_CODE, Result.ERR_MSG);
        }
        return result;
    }
}
